﻿#include "catnetwork.h"
#include <QHostInfo>
#include <QNetworkInterface>

CatNetWork::CatNetWork(QObject *parent)
    : QObject(parent)
{
    Update_NetworkInterface();
}

CatNetWork::~CatNetWork()
{

}

void CatNetWork::lookupHost(QString host)
{
    QHostInfo::lookupHost(host, this, [=](const QHostInfo &host){
        if(host.error() != QHostInfo::NoError)
        {
            emit LookupHost(host.errorString());
        }

        foreach(const QHostAddress &address, host.addresses())
        {
            emit LookupHost(address.toString());
        }

    });

}

QList<QString> CatNetWork::GetAllIpaddressIpv4() const
{
    return m_lIpaddressIpv4;
}

QList<QString> CatNetWork::GetAllIpaddressIpv6() const
{
    return m_lIpaddressIpv6;
}

QList<QString> CatNetWork::GetAllNetWorkName() const
{
    return m_lName;
}

QString CatNetWork::GetNetWorkHardware(QString name)
{
    QString hardware = "";

    for(auto temp : m_lNetworkimterface)
    {
        if(temp.Name == name)
        {
            hardware = temp.Hardware;
        }
    }

    return hardware;
}

QString CatNetWork::GetNetWorkHardware(QString ipaddress, CatNetWork::IP ip)
{
    QString hardware = "";
    for(auto temp : m_lNetworkimterface)
    {
        for(auto tempentry : temp.Entry)
        {
            if(ip == CatNetWork::IP::V4 && ipaddress == tempentry.IpAddressV4)
            {
                hardware = temp.Hardware;
            } else if(ip == CatNetWork::IP::V6 && ipaddress == tempentry.IpAddressV6)
            {
                hardware = temp.Hardware;
            }
        }
    }
    return hardware;
}

QString CatNetWork::GetNetWorkBroadcast(QString ipaddress, CatNetWork::IP ip)
{
    QString broadcast = "";
    for(auto temp : m_lNetworkimterface)
    {
        for(auto tempentry : temp.Entry)
        {
            if(ip == CatNetWork::IP::V4 && ipaddress == tempentry.IpAddressV4)
            {
                broadcast = tempentry.Broadcast;
            } else if(ip == CatNetWork::IP::V6 && ipaddress == tempentry.IpAddressV6)
            {
                broadcast = tempentry.Broadcast;
            }
        }
    }
    return broadcast;
}

QString CatNetWork::GetNetWorkNetMask(QString ipaddress, CatNetWork::IP ip)
{
    QString netmask = "";
    for(auto temp : m_lNetworkimterface)
    {
        for(auto tempentry : temp.Entry)
        {
            if(ip == CatNetWork::IP::V4 && ipaddress == tempentry.IpAddressV4)
            {
                netmask = tempentry.NetMask;
            } else if(ip == CatNetWork::IP::V6 && ipaddress == tempentry.IpAddressV6)
            {
                netmask = tempentry.NetMask;
            }
        }
    }
    return netmask;
}

void CatNetWork::InitNetworkInterface()
{
    // 获取所有网络接口的列表
    QList<QNetworkInterface> list = QNetworkInterface::allInterfaces();
    // 遍历每一个网络接口
    foreach(QNetworkInterface interface, list)
    {
        NETWORKINTERFACE temp;
        // 接口名
        temp.Name = interface.name();
        m_lName.push_back(temp.Name);

        // 硬件地址
        temp.Hardware = interface.hardwareAddress();

        // 获取IP地址条目列表，每个条目包含一个IP地址，子网掩码和广播地址
        QList<QNetworkAddressEntry> entryList = interface.addressEntries();

        foreach(QNetworkAddressEntry entry, entryList)
        {
            NETWORKADDRESSENTRY EntryTemp;
            // IP 地址
            QString ip = entry.ip().toString();
            if(!ip.isEmpty())
            {
                if(entry.ip().protocol() == QAbstractSocket::IPv4Protocol)
                {
                    EntryTemp.IpAddressV6 = "";
                    EntryTemp.IpAddressV4 = ip;
                    m_lIpaddressIpv4.push_back(EntryTemp.IpAddressV4);
                } else if(entry.ip().protocol() == QAbstractSocket::IPv6Protocol) {
                    EntryTemp.IpAddressV4 = "";
                    EntryTemp.IpAddressV6 = ip;
                    m_lIpaddressIpv6.push_back(EntryTemp.IpAddressV6);
                }
            }
            // 子网掩码
            EntryTemp.NetMask = entry.netmask().toString();
            // 广播地址
            EntryTemp.Broadcast = entry.broadcast().toString();

            temp.Entry.push_back(EntryTemp);
        }

        m_lNetworkimterface.push_back(temp);
    }

}

void CatNetWork::Update_NetworkInterface()
{
    m_lNetworkimterface.clear();
    m_lName.clear();
    m_lIpaddressIpv4.clear();
    m_lIpaddressIpv6.clear();
    InitNetworkInterface();
    emit update_NetWorkIpv4(m_lIpaddressIpv4);
    emit update_NetWorkIpv6(m_lIpaddressIpv6);
}
